<?php
/**
 * The applications main class must implement the 
 * application interface.
 * 
 * 
 * @author embrace.toolkit
 * @version $Id$
 */
class EmbraceBugTracker implements Application {
	/**
	 * Contains config information.
	 * @var Array
	 */
	private $configuration;
	
	/**
	 * File names to ignore on scan.
	 * @var Array
	 */
	private $ignore; 
		
	/**
	 * Creates an EBT instance.
	 * @param $confPath String path to the ini file.
	 */
	public function EmbraceBugTracker($confPath) {
		$this->configure($confPath);
		$this->ignore = array(".", "..", ".DS_Store", ".svn");
	}
	
	/**
	 * (non-PHPdoc)
	 * @see ebt/include/definition/Application#configure()
	 */
	public function configure($confPath) {
		$this->configuration = parse_ini_file(
			$confPath . get_class($this) . ".ini", 
			true
		);
	}
	
	/**
	 * (non-PHPdoc)
	 * @see ebt/include/definition/Application#getRoot()
	 */
	public function getRoot() {
		return $this->getDirectory("root");
	}
	
	/**
	 * (non-PHPdoc)
	 * @see ebt/include/definition/Application#getTemp()
	 */
	public function getTemp() {
		return $this->getDirectory("temp");
	}	
	
	/**
	 * Returns the specified path including app specific subdir etc. 
	 * @param $dirName String the name of the directory wihtin the
	 * configuration file.
	 * @return String the app specifirfc path,
	 */
	protected function getDirectory($dirName) {
		return $this->configuration["directories"][$dirName];	
	}
	
	/**
	 * (non-PHPdoc)
	 * @see ebt/include/definition/Application#getData()
	 */
	public function getProjects() {
		$dataPath = $this->getDataPath();
		return $this->recurse($dataPath);
	}
	
	/**
	 * CLoned from examples @php.net/scandir
	 * @param $outerDir
	 * @return unknown_type
	 */
	function recurse($outerDir) {
		$dirs = array_diff(scandir($outerDir), $this->ignore);
	    $dir_array = Array();
	    foreach( $dirs as $d ){
	        if(is_dir($outerDir."/".$d)) 
	        	$dir_array[ $d ] = $this->recurse($outerDir."/".$d);
	        else 
	        	$dir_array[ $d ] = $d;
	    }
	    return $dir_array; 
	}
	
	/**
	 * Returns the data pathto the projects.
	 * @return String
	 */
	public function getDataPath() {
		return $_SERVER["DOCUMENT_ROOT"] . 
			$this->configuration["directories"]["root"] . "/" .
			$this->configuration["directories"]["data"] . "/";
	}

	/**
	 * Returns a ticket type setting from configuration for the 
	 * given name.
	 * @param $tagName name of the tag.
	 * @return String
	 */
	protected function getTag($tagName) {
		return $this->configuration["ticket_types"][$tagName];
	}
	
	public function getOpenTag() {
		return $this->getTag("open");
	}
	
	public function getNewTag() {
		return $this->getTag("new");
	}
	
	public function getClosedTag() {
		return $this->getTag("closed");
	}
	
	public function getDeprecatedTag() {
		return $this->getTag("deprecated");
	}
	
	public function getPendingTag() {
		return $this->getTag("pending");
	}
	
	public function getFeatureTag() {
		return $this->getTag("feature");
	}
}
?>